#!/bin/bash
# https://github.com/girst/hardpass-sendHID
# https://www.kernel.org/doc/Documentation/usb/gadget_configfs.txt
# https://github.com/c4software/pi-as-keyboard

# echo '' > /sys/kernel/config/usb_gadget/g1/UDC	# 停止HID模拟，sudo su下执行
modprobe libcomposite

cd /sys/kernel/config/usb_gadget/
mkdir -p g1
cd g1
echo 0x1d6b > idVendor	# 厂商ID为 The Linux Foundation
echo 0x0104 > idProduct	# 产品ID为 Multifunction Composite Gadget
echo 0x0100 > bcdDevice	# 设备的版本号为 v1.0.0
echo 0x0200 > bcdUSB	# 所使用的USB版本为 2.0

mkdir -p strings/0x409
echo "fedcba9876543210" > strings/0x409/serialnumber	# 设备的序列号
echo "Raspberry" > strings/0x409/manufacturer			# 厂商名
echo "Zero" > strings/0x409/product						# 产品名

N="usb0"
mkdir -p functions/hid.$N
echo 1 > functions/hid.usb0/protocol		# 该接口使用的协议0=none, 1=keyboard, 2=mouse
echo 1 > functions/hid.usb0/subclass		# 该接口所用的子类 1=BOOT, 0=no boot
echo 8 > functions/hid.usb0/report_length	# 设备描述符的长度，8=keyboard , 4=mouse

# 键盘设备描述符
echo -ne \\x05\\x01\\x09\\x06\\xa1\\x01\\x05\\x07\\x19\\xe0\\x29\\xe7\\x15\\x00\\x25\\x01\\x75\\x01\\x95\\x08\\x81\\x02\\x95\\x01\\x75\\x08\\x81\\x03\\x95\\x05\\x75\\x01\\x05\\x08\\x19\\x01\\x29\\x05\\x91\\x02\\x95\\x01\\x75\\x03\\x91\\x03\\x95\\x06\\x75\\x08\\x15\\x00\\x25\\x65\\x05\\x07\\x19\\x00\\x29\\x65\\x81\\x00\\xc0 > functions/hid.usb0/report_desc

# 鼠标设备描述符
#echo -ne \\x05\\x01\\x09\\x02\\xa1\\x01\\x09\\x01\\xa1\\x00\\x05\\x09\\x19\\x01\\x29\\x03\\x15\\x00\\x25\\x01\\x95\\x03\\x75\\x01\\x81\\x02\\x95\\x01\\x75\\x05\\x81\\x01\\x05\\x01\\x09\\x30\\x09\\x31\\x09\\x38\\x15\\x81\\x25\\x7f\\x75\\x08\\x95\\x02\\x81\\x06\\xc0\\xc0 > functions/hid.usb0/report_desc

C=1
mkdir -p configs/c.$C/strings/0x409
echo "Config $C: ECM network" > configs/c.$C/strings/0x409/configuration 
echo 250 > configs/c.$C/MaxPower	# 设备需要从总线上获取多少电流，单位为2mA，0x96表示300mA
ln -s functions/hid.$N configs/c.$C/

ls /sys/class/udc > UDC
